package com.example.geohash.web;

import com.example.geohash.service.RedisGeohashService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * liuyu07
 */
@RestController
public class RedisGeohashController {

    @Autowired
    RedisGeohashService redisGeohashService;

    private static final Logger logger = LoggerFactory.getLogger(RedisGeohashController.class);

    ExecutorService threadPool = Executors.newFixedThreadPool(20);

    /**
     * 初始化100w数据
     * @return
     */
    @RequestMapping("set1m")
    public Map set1m(){
        for(int i=0; i < 1000000; i++){
            double x =  new Random().nextDouble()*180;
            double y =  new Random().nextDouble()*85;
            final  int z = i;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    redisGeohashService.insertStation("station"+z,x,y);
                }
            });

        }
        logger.info("初始化完成");
        return new HashMap();
    }

    /**
     * 计算附近的油站
     * @param x 经度
     * @param y 纬度
     * @param r 距离
     * @return
     */
    @RequestMapping("getNearlyStation")
    public Map getNearlyStation(float x, float y, int r){
        logger.info("参数：{},{}",x,y);
        List list = redisGeohashService.getNearlyStation(x,y,r);
        logger.info("结果={}",list);
        HashMap map = new HashMap();
        map.put("data", list);
        return map;
    }
}
